package com.ad.pet.app.account.share;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ad.pet.R;
import com.ad.pet.models.remote.share.ShareIconBean;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by SEELE on 2019/7/4.
 */

public class ShareDialog extends Dialog {
    @BindView(R.id.rv_share)
    RecyclerView rvShare;
    @BindView(R.id.ll_cancel)
    RelativeLayout llCancel;
    private Context mContext;
    private List<ShareIconBean> listIcons = new ArrayList<>();
    private ShareIconAdapter iconAdapter;

    private String mTitle;
    private Bitmap mBitmap;
    private String url;
    private String describe;


    public ShareDialog(@NonNull Context context) {
        super(context, R.style.CommonDialog);
        this.mContext = context;
    }

    public ShareDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected ShareDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    /**
     * 分享
     */
    public void share(String url, String title, Bitmap mShareBitmap, String describe) {
        mTitle = title;
        this.url = url;
        mBitmap = mShareBitmap;
        this.describe = describe;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog_share);
        ButterKnife.bind(this);
        setUpWindow();
        initView();
    }

    private void initView() {

        listIcons.add(new ShareIconBean(R.drawable.umeng_socialize_wechat, "微信", SHARE_MEDIA.WEIXIN));
        listIcons.add(new ShareIconBean(R.drawable.umeng_socialize_wxcircle, "朋友圈", SHARE_MEDIA.WEIXIN_CIRCLE));
        listIcons.add(new ShareIconBean(R.drawable.umeng_socialize_qq, "QQ", SHARE_MEDIA.QQ));
        listIcons.add(new ShareIconBean(R.drawable.umeng_socialize_facebook, "Facebook", SHARE_MEDIA.FACEBOOK));
        GridLayoutManager manager = new GridLayoutManager(mContext, 4);
        rvShare.setLayoutManager(manager);
        iconAdapter = new ShareIconAdapter(null);
        rvShare.setAdapter(iconAdapter);
        iconAdapter.setNewData(listIcons);
        iconAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ShareIconBean bean = (ShareIconBean) adapter.getItem(position);

                UMImage image = new UMImage(mContext, mBitmap);
                UMWeb web = null;//分享的连接

                switch (bean.getType()) {
                    case QQ:
                        web=new UMWeb(url+"&channel=3");
                        web.setTitle(mTitle);//标题
                        web.setThumb(image);  //缩略图
                        web.setDescription(describe);//描述
                        new ShareAction((Activity) mContext).setPlatform(SHARE_MEDIA.QQ)
                                .withMedia(web)
                                .setCallback(umShareListener)
                                .share();
                        break;
                    case FACEBOOK:
                        web=new UMWeb(url+"&channel=4");
                        web.setTitle(mTitle);//标题
                        web.setThumb(image);  //缩略图
                        web.setDescription(describe);//描述
                        new ShareAction((Activity) mContext).setPlatform(SHARE_MEDIA.FACEBOOK)
                                .withMedia(web)
                                .setCallback(umShareListener)
                                .share();
                        break;
                    case WEIXIN:
                        web=new UMWeb(url+"&channel=1");
                        web.setTitle(mTitle);//标题
                        web.setThumb(image);  //缩略图
                        web.setDescription(describe);//描述
                        new ShareAction((Activity) mContext).setPlatform(SHARE_MEDIA.WEIXIN)
                                .withMedia(web)
                                .setCallback(umShareListener)
                                .share();
                        break;
                    case WEIXIN_CIRCLE:
                        web=new UMWeb(url+"&channel=2");
                        web.setTitle(mTitle);//标题
                        web.setThumb(image);  //缩略图
                        web.setDescription(describe);//描述
                        new ShareAction((Activity) mContext).setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                                .withMedia(web)
                                .setCallback(umShareListener)
                                .share();
                        break;
                    default:
                        break;
                }

                if (ShareDialog.this.isShowing()) {
                    ShareDialog.this.dismiss();
                }


            }
        });

    }

    //设置Dialog显示的位置
    private void setUpWindow() {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
    }

    @OnClick(R.id.ll_cancel)
    public void onViewClicked() {
        if (this.isShowing()) {
            this.dismiss();
        }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {
            LogUtils.d("onStart");


        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            LogUtils.d("onResult");

        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            LogUtils.d("onError");

        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            LogUtils.d("onCancel");

        }
    };
}